package com.austin.smp.common.util;

import lombok.Data;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author austin
 * @since austin/6/28 19:50
 */

@Data
public class FTPUtil {

    private static final Logger log = LoggerFactory.getLogger(FTPUtil.class);


    private String ip;              //FTP服务器ip,ip=host,也有host
    private int port;            //FTP服务器端口
    private String username;        //FTP服务器登录账号
    private String password;        //FTP服务器登录密码
    private FTPClient ftpClient;    //FTP客户端，通过客户端来连接到FTP服务器


    private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip");
    private static String ftpUsername = PropertiesUtil.getProperty("ftp.username");
    private static String ftpPassword = PropertiesUtil.getProperty("ftp.password");

    public FTPUtil(String ip, int port, String username, String password) {
        this.ip = ip;
        this.port = port;
        this.username = username;
        this.password = password;
    }

    public static boolean uploadFile(List<File> fileList) throws IOException {
        FTPUtil ftpUtil = new FTPUtil(ftpIp, 21, ftpUsername, ftpPassword);
        log.info("开始连接到ftp服务器");
        boolean result = ftpUtil.uploadFile("img", fileList);
        log.info("开始连接到ftp服务器,结束上传，上传结果：{}", result);
        return result;
    }

    /**
     * 文件上传
     *
     * <p>
     * 过程：上传获下载都是先将文件流先拿到，然后将文件流读到缓存，然后程序从
     * 缓存中将所需的内容读取出放进要导入的文件中。
     * </p>
     *
     * @param remotePath 上传路径
     * @param fileList   上传的文件
     * @return
     */
    public boolean uploadFile(String remotePath, List<File> fileList) throws IOException {
        boolean uploadResult = true;
        FileInputStream fis = null;
        if (this.connectToFTPServer(this.ip, this.port, this.username, this.password)) {
            //连接到FTP服务器成功
            try {
                ftpClient.changeWorkingDirectory(remotePath);   //切换工作路径
                ftpClient.setBufferSize(1024);
                //设置每次读取文件流时缓存数组的大小,上传过程：文件读读到缓存->程序从缓存将内容读取出来到导入文件中，一般设为1024的倍数
                ftpClient.setControlEncoding("utf-8");
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  //二进制类型
                ftpClient.enterLocalActiveMode();   //Ftp主动模式
                //开始上传
                for (File fileItem : fileList) {
                    fis = new FileInputStream(fileItem);
                    ftpClient.storeFile(fileItem.getName(), fis);
                }
            } catch (IOException e) {
                log.error("上传文件异常");
                uploadResult = false;
                e.printStackTrace();
            } finally {
                fis.close();    //关闭流
                ftpClient.disconnect(); //关闭ftp服务器
            }
        }
        return uploadResult;
    }

    /**
     * 连接得到FTP服务器方法
     *
     * @param ip
     * @param port
     * @param username
     * @param password
     * @return
     */
    public boolean connectToFTPServer(String ip, int port, String username, String password) {
        boolean isSuccess = false;
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip);
            isSuccess = ftpClient.login(username, password);

        } catch (IOException e) {
            log.error("连接到服务器异常");
        }
        return isSuccess;
    }
}
